import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import { getAdminUserInfoAPI } from "@/api/admin";
import { getUserInfoAPI } from "@/api/./user";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: "",
    touritToken: "",
    userInfo: {},
    touristInfo: {},
  },
  getters: {
    nickname: (state) => state.userInfo.nickname,
    username: (state) => state.userInfo.username,
    user_pic: (state) => state.userInfo.user_pic,
    Touristnickname: (state) => state.touristInfo.nickname,
    Touristusername: (state) => state.touristInfo.username,
    Touristuser_pic: (state) => state.touristInfo.user_pic,
  },
  mutations: {
    updateToken(state, newToken) {
      state.token = newToken;
    },
    updateUsreInfo(state, info) {
      state.userInfo = info;
    },
    updateTouristToken(state, newToken) {
      state.touritToken = newToken;
    },
    updateTouristInfo(state, info) {
      state.touristInfo = info;
    },
  },
  actions: {
    async initUserInfo(store) {
      const { data: res } = await getAdminUserInfoAPI();
      if (res.status === 0) {
        store.commit("updateUsreInfo", res.data);
      }
    },
    async initGetTouristInfo(store) {
      const { data: res } = await getUserInfoAPI();
      if (res.status === 0) {
        store.commit("updateTouristInfo", res.data);
      }
    },
  },
  modules: {},
  // 配置为 vuex 的插件
  plugins: [createPersistedState()],
});
